<template>
  <!-- No changes to template section -->
</template>

<script>
import axios from 'axios';

export default {
  // No changes to component options

  methods: {
    async login() {
      try {
        const response = await axios.post('/api/auth/login', {
          username: this.loginForm.username,
          password: this.loginForm.password
        });
        
        console.log('登录响应:', response);
        
        if (response.data && (response.data.code === 200 || response.data.status === 'success')) {
          const token = response.data.token || response.data.data?.token;
          
          if (token) {
            // 保存token和用户信息
            localStorage.setItem('token', token);
            
            // 保存教师信息
            if (response.data.data && response.data.data.teacher) {
              const teacher = response.data.data.teacher;
              console.log('登录成功，获取到教师信息:', teacher);
              
              localStorage.setItem('teacherId', teacher.id || teacher.teacherId);
              localStorage.setItem('teacherName', teacher.name);
              
              // 规范化并保存教师学科
              if (teacher.subject) {
                const normalizedSubject = this.normalizeSubject(teacher.subject);
                localStorage.setItem('teacherSubject', normalizedSubject);
                console.log(`已保存教师学科信息 - 原始值: "${teacher.subject}", 规范化后: "${normalizedSubject}"`);
              } else {
                console.warn('教师信息中没有学科字段:', teacher);
              }
            }
            
            // 登录成功后跳转到首页
            this.$router.push('/');
          } else {
            console.error('登录响应中没有token:', response.data);
            this.$message.error('登录失败：服务器返回数据格式不正确');
          }
        } else {
          console.error('登录失败:', response.data);
          this.$message.error(response.data.message || '登录失败，请检查用户名和密码');
        }
      } catch (error) {
        console.error('登录请求错误:', error);
        this.$message.error('登录失败：' + (error.response?.data?.message || error.message || '未知错误'));
      }
    },
    
    // 标准化学科代码
    normalizeSubject(subject) {
      if (!subject) return '';
      
      // 转为小写
      const lowerSubject = String(subject).toLowerCase();
      console.log(`[Login] 标准化学科输入: "${subject}", 转小写后: "${lowerSubject}"`);
      
      // 中文学科名称映射
      const zhSubjectMap = {
        '语文': 'chinese',
        '数学': 'math',
        '英语': 'english',
        '物理': 'physics',
        '化学': 'chemistry',
        '生物': 'biology',
        '政治': 'politics',
        '历史': 'history',
        '地理': 'geography'
      };
      
      // 如果是中文学科名称，转换为英文代码
      if (zhSubjectMap[subject]) {
        return zhSubjectMap[subject];
      }
      
      // 检查小写后的中文名称
      if (zhSubjectMap[lowerSubject]) {
        return zhSubjectMap[lowerSubject];
      }
      
      // 已经是标准学科代码
      const validSubjects = ['chinese', 'math', 'english', 'physics', 'chemistry', 
                           'biology', 'politics', 'history', 'geography'];
      if (validSubjects.includes(lowerSubject)) {
        return lowerSubject;
      }
      
      // 处理简写或部分匹配
      if (lowerSubject.includes('chin') || lowerSubject.includes('yuwen') || lowerSubject.includes('语文')) {
        return 'chinese';
      }
      if (lowerSubject.includes('math') || lowerSubject.includes('shuxue') || lowerSubject.includes('数学')) {
        return 'math';
      }
      
      // 返回原值
      return lowerSubject;
    },
  }
}
</script>

<style>
  /* No changes to style section */
</style> 